<?php

#################################################################
#								HTML
#################################################################

/**
 * Layout to link JS scripts to a layout, registed as 'js', JS file registed as 'blg.js'
 */

if($_CONTEXT=='HTML'){
		
	global $_CACHE;
	
	$key= $_JS_STACK->key();
	
	/*** FILE JS CODE ***/
	if($_CACHE->has_expired($key)){			
		$js_files= $_JS_STACK->extract_all(true);			
		$js_code= array();
		if(is_array($js_files))
			foreach($js_files as $js_file)
				$js_code[] = file_get_contents($js_file);

		$js_code= implode("\r\n ", $js_code);
	
		$_CACHE->set($key, $js_code);			
	}	
	echo '<script src="'.blg_get_site_url().'blg.js?k='.$key.'" type="text/javascript"></script>'."\n\t";
	
	/*** INLINE JS CODE ***/
	$key= uniqid('js_inline_');
	$js_code= $_JS_CODE->code();	
	$_CACHE->set($key, $js_code, 0);
	echo '<script src="'.blg_get_site_url().'blg.js?k='.$key.'" type="text/javascript"></script>';
	
}
#################################################################
#								JS
#################################################################
elseif($_CONTEXT=='JS'){
	global $_CACHE;
	
	$key= blg_get_input('k');
	$info= $_CACHE->get_info($key);
	
	header("Expires: " . gmdate("D, d M Y H:i:s", $info['time_cache_expires']) . " GMT");
	header("Last-Modified: " . gmdate('D, d M Y H:i:s', $info['time_written'] ) . " GMT" );	
	header("Cache-Control: max-age=".$info['duration']);
	
	echo $_CACHE->get($key);
	
	if($info['duration'] <= 0)
		$_CACHE->clean($key);
}
#################################################################
#								CSS
#################################################################
elseif($_CONTEXT=='CSS'){ ?>


<?php }